package com.example.wendao.view.ui.fragment;


import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.databinding.FragmentTalkBinding;
import com.example.wendao.view.adapter.TalkListAdapter;
import com.example.wendao.viewmodel.ForumViewModel;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class TalkFragment extends Fragment {
    private TalkListAdapter adapter = new TalkListAdapter();
    private ForumViewModel forumViewModel = null;
    private RecyclerView rv;
    private SwipeRefreshLayout srl;
    public TalkFragment() {}

    private int type;

    public TalkFragment setType(int type) {
        this.type = type;
        return this;
    }

    private FragmentTalkBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_talk, container, false);
        rv = binding.talkRecyclerview;
        srl = binding.talkSwiperefreshlayout;
        forumViewModel = new ViewModelProvider(App.getInstance()).get(ForumViewModel.class);
        if (type == 6){
            forumViewModel.getAllForum().observe(this.getViewLifecycleOwner(), adapter::submitList);
        } else {
            forumViewModel.getAllForumByTagId(type).observe(this.getViewLifecycleOwner(), adapter::submitList);
        }
        binds();
        PageUtils.initRecyclerView(rv, adapter, new LinearLayoutManager(this.getActivity()));
        return binding.getRoot();
    }

    private void binds() {
        // 模拟下拉刷新
        srl.setOnRefreshListener(() -> {
            srl.setRefreshing(true);
            Observable.just(0)
                    .delay(1, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(i -> {
                        srl.setRefreshing(false);
                        adapter.submitList(null);
                        if (type == 6){
                            forumViewModel.getAllForum().observe(getActivity(), adapter::submitList);
                        } else {
                            forumViewModel.getAllForumByTagId(type).observe(getActivity(), users -> adapter.submitList(users));
                        }
                    }).subscribe();
        });
    }
}

